package com.alan.springbootbase.controller;


import com.alan.springbootbase.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 */
@Slf4j
@RestController
public class RedisController {

    @Autowired
    private RedisUtil redisUtil;


    @GetMapping("/set")
    public String testSet(){
        log.info("******testSetIfNotExists******");
        int time = 10;//超时时间写死为10秒
        String s = redisUtil.set("aaaaa","111111");
        log.info("******s=" + s);
        return s;
    }
    @GetMapping("/get")
    public String testGet(){
        log.info("******testSetIfNotExists******");
        int time = 10;//超时时间写死为10秒
        String s = redisUtil.get("aaaaa");
        log.info("******s=" + s);
        return s;
    }
    @GetMapping("/setTime")
    public String testSetTime(){
        log.info("******testSetIfNotExists******");
        int time = 20;//超时时间写死为10秒
        String s = redisUtil.set("bbbbb","2222",time);
        log.info("******s=" + s);
        return s;
    }
    @GetMapping("/getTime")
    public String testGetTime(){
        log.info("******testSetIfNotExists******");
        int time = 20;//超时时间写死为10秒
        String s = redisUtil.get("bbbbb");
        log.info("******s=" + s);
        return s;
    }



    @GetMapping("/testDistributedLock")
    public String testSetIfNotExists(@RequestParam("name") String name,
                                     @RequestParam("value") int age){
        log.info("******testSetIfNotExists******");

        int time = 10;//超时时间写死为10秒
        String ageStr = String.valueOf(age);
        String s = redisUtil.getDistributedLock(name, ageStr, time);

        log.info("******s是否为null:" + (s == null));
        log.info("******s=" + s);

        return s;
    }


}
